package com.gm.commonlib.util.viewUtils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.IBinder;
import android.text.InputFilter;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

public class KeyboardUtils {


	public static boolean isActive(Activity activity, TextView searchEditText, View otherView) {
		InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager.isActive(searchEditText)) {//因为是在fragment下，所以用了getView()获取view，也可以用findViewById（）来获取父控件
			otherView.requestFocus();//使其它view获取焦点.这里因为是在fragment下,所以便用了getView(),可以指定任意其它view
			inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			return true;
		}
		return false;
	}

	public static void hideKeyboard(InputMethodManager manager, View view) {
		if(manager != null && view != null) {
			manager.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
		}
	}

	public static boolean hideKeyboard(Context context, View view) {
		if(context != null && view != null) {
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (imm != null) {
				imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
				return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
			}
		}
		return false;
	}

	/**
	 * hide inputMethod
	 */
	public static void hideKeyboard(Activity mActionBarActivity) {
		InputMethodManager inputMethodManager = (InputMethodManager) mActionBarActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager != null) {
			View localView = mActionBarActivity.getCurrentFocus();
			if (localView != null && localView.getWindowToken() != null) {
				IBinder windowToken = localView.getWindowToken();
				inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
			}
		}
	}

	public static void hideKeyboard(View view) {
		if(view == null) {
			return;
		}
		InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		if(imm != null) {
			imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}

	public static void showKeyboard(InputMethodManager manager, View view) {
		if(manager != null && view != null) {
			manager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
		}
	}

	//TODO 点击点在v所在的窗口区域
	public static boolean isShouldHideInput(View v, MotionEvent event) {
		if (v instanceof EditText) {
			int[] leftTop = {0, 0};
			v.getLocationInWindow(leftTop);
			Rect rect = new Rect(leftTop[0], leftTop[1], leftTop[0] + v.getWidth(), leftTop[1] + v.getHeight());
			if (rect.contains((int)event.getX(), (int)event.getY())) {
				//TODO 保留点击EditText的事件
				return false;
			}
		}
		return true;
	}

	/**
	 * TODO 点击EditText之外空白区域，隐藏键盘
	 * @param context
	 * @param ev
     * @return
     */
	public static void handleEmptySpaceClickedEvent(Activity context, MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			View v = context.getCurrentFocus();
			if (isShouldHideInput(v, ev)) {
				//隐藏键盘时，其他控件不响应点击事件==》注释则不拦截点击事件
				hideKeyboard(context, v);
			}
		}
	}

	public static void setInputTypeChineseName(EditText editText, InputFilter[] inputFilters) {
		ViewUtil.setKeyboardTypeToText(editText);
		ViewUtil.setFilters(editText, inputFilters);
	}

	public static void setInputTypeIDCard(EditText editText, InputFilter[] inputFilters) {
		ViewUtil.setKeyboardTypeToText(editText);
		ViewUtil.setFilters(editText, inputFilters);
	}

	public static void setInputTypeTelephone(EditText editText, InputFilter[] inputFilters) {
		ViewUtil.setKeyboardTypeToNumber(editText);
		ViewUtil.setFilters(editText, inputFilters);
	}

	public static void setInputTypeBankCardNumber(EditText editText, InputFilter[] inputFilters) {
		ViewUtil.setKeyboardTypeToNumber(editText);
		ViewUtil.setFilters(editText, inputFilters);
	}
}